package com.weitu.permission.post.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.department.entity.Department;
import com.weitu.permission.post.entity.Post;
import com.weitu.permission.post.service.PostService;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangqingxi 2017/6/6
 */
@Controller
@RequestMapping("permission/post")
public class PostController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "permission/post";

    private final PostService postService;

    @Autowired
    public PostController(PostService postService) {
        this.postService = postService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView listView() {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName(MODULE_BATH_PATH + "/list");

        return modelAndView;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addView(String deptId) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("deptId", deptId);
        modelAndView.setViewName(MODULE_BATH_PATH + "/add");
        return modelAndView;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editView(String id) {

        Post post = postService.findById(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("post", post);
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");

        return modelAndView;
    }


    @ResponseBody
    @RequestMapping("/findByParam")
    public Object findByParam(HttpServletRequest request) {


        Map<String, Object> map = new HashMap<String, Object>();

        //获取前台参数
        putParametersIntoMap(map);

        Integer totalNum = postService.findByParamCount(map);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);

        PageView pageView = pagination.getPageView();//生成pageView分页对象
        map.put("pageView", pageView);

        List<Post> postList = postService.findByParam(map);

        return pagination.flushDataGridData(postList);
    }

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @SystemLog(module = "岗位管理", methods = "添加岗位")
    public AjaxResult add() {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getAddBasicFieldMap(paramMap);

        Integer result = postService.add(paramMap);


        return resultAjaxResult(result);
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @SystemLog(module = "岗位管理", methods = "编辑岗位")
    public AjaxResult edit() {

        return update();
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @SystemLog(module = "岗位管理", methods = "删除岗位")
    public AjaxResult delete() {

        return update();
    }

    /**
     * 编辑岗位的方法
     *
     * @return 编辑结果
     */
    public AjaxResult update() {


        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数

        getEditBasicFieldMap(paramMap);

        Integer result = postService.updateById(paramMap);
        return resultAjaxResult(result);
    }

    /**
     * 处理返回参数
     *
     * @param result 返回值
     * @return 返回值
     */
    private AjaxResult resultAjaxResult(Integer result) {
        AjaxResult ajaxResult = new AjaxResult();
        if (result > 0) {
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("操作成功");
        }
        return ajaxResult;
    }

    /**
     * 检验添加页面的code 是否可用
     *
     * @param code code
     * @return 存在返回false 不存在为true
     */
    @ResponseBody
    @RequestMapping("/findByCode")
    public AjaxResult findByCode(String code) {

        Post post = postService.findByCode(code.trim());

        AjaxResult ajaxResult = new AjaxResult();
        if (null == post) {
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("当前岗位编号不存在");
        } else {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("当前岗位编号已存在");
        }

        return ajaxResult;
    }

    /**
     * 根据部门查询岗位
     * @param deptId 部门id
     * @return 岗位信息
     */
    @ResponseBody
    @RequestMapping("/findByDeptId")
    public List<Map> findByDeptId(String deptId) {

        return postService.findByDeptId(deptId);


    }
}
